package ai.people.platform.oss.service;

import ai.people.netmon.framework.domain.oss.vo.UploadVO;
import ai.people.netmon.framework.exception.enums.FileExceptionEnum;
import ai.people.netmon.framework.exception.enums.PictureExceptionEnum;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.netmon.utils.FileUtils;
import ai.people.netmon.utils.PictureUtils;
import ai.people.netmon.utils.StringPool;
import ai.people.netmon.utils.UUIDUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * @author yuanqinglong
 * @date 2022/4/28 17:30
 */
public interface OssStorageService {


    /**
     * 上传
     *
     * @param multipartFile 多部分文件
     * @return {@link UploadVO}
     * @throws Exception 异常
     */
    UploadVO uploadPicture(MultipartFile multipartFile) throws Exception;

    /**
     * 上传文件
     *
     * @param multipartFile 多部分文件
     * @return {@link UploadVO}
     * @throws Exception 异常
     */
    UploadVO uploadFile(MultipartFile multipartFile) throws Exception;


    /**
     * 获取文件
     *
     * @param fileName           文件名称
     * @param httpServletRequest http servlet请求
     * @param response           响应
     * @throws Exception 异常
     */
    void write(String fileName, HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception;


    /**
     * 过程名字
     *
     * @param fileName 文件名称
     * @return {@link String}
     */
    default String createPictureName(String fileName) {
        String originalFilename = Optional.ofNullable(fileName).orElse(StringPool.EMPTY);
        AssertUtils.isTrue(PictureUtils.checkSuffix(originalFilename), PictureExceptionEnum.IMG_SUFFIX_NOT_SUPPORT);
        return UUIDUtils.getShort() + originalFilename.substring(originalFilename.lastIndexOf(StringPool.DOT));
    }

    /**
     * 创建文件名称
     *
     * @param fileName 文件名称
     * @return {@link String}
     */
    default String createFileName(String fileName) {
        String originalFilename = Optional.ofNullable(fileName).orElse(StringPool.EMPTY);
        AssertUtils.isTrue(FileUtils.checkSuffix(originalFilename), FileExceptionEnum.FILE_SUFFIX_NOT_SUPPORT);
        return UUIDUtils.getShort() + StringPool.UNDERSCORE + originalFilename;
    }


    /**
     * 设置响应头
     *
     * @param httpServletRequest  http servlet请求
     * @param httpServletResponse http servlet响应
     * @param fileName
     */
    default void setPictureResponseHeaders(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String fileName) {
        httpServletResponse.setContentType(MediaType.IMAGE_JPEG_VALUE);
        String accept = httpServletRequest.getHeader(HttpHeaders.ACCEPT);
        httpServletResponse.setHeader(HttpHeaders.ACCEPT, accept);
    }

    /**
     * 设置文件响应标头
     *
     * @param httpServletRequest  http servlet请求
     * @param httpServletResponse http servlet响应
     * @param fileName            文件名称
     */
    default void setFileResponseHeaders(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String fileName) {
        httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        httpServletResponse.setCharacterEncoding("utf-8");
        int i = fileName.indexOf(StringPool.UNDERSCORE);
        String oriName = fileName.substring(i + 1);
        httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + oriName);
        String accept = httpServletRequest.getHeader(HttpHeaders.ACCEPT);
        httpServletResponse.setHeader(HttpHeaders.ACCEPT, accept);
    }


    /**
     * 写文件
     *
     * @param fileName            文件名称
     * @param httpServletRequest  http servlet请求
     * @param httpServletResponse http servlet响应
     */
    void writeFile(String fileName, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception;

}
